package us.icitap.timsportal.management;

import java.util.Date;

import us.icitap.timsportal.ejb.PortalBeanLocal;
import us.icitap.timsportal.entity.Officer;
import us.icitap.timsportal.entity.User;
import us.icitap.timsportal.notifications.ConfirmDialogActions;
import us.icitap.timsportal.notifications.TimsPortalConfirmDialog;
import us.icitap.timsportal.resources.TimsPortalLabels;
import us.icitap.timsportal.util.PortalNotification;
import us.icitap.timsportal.web.custom.PNativeButton;
import us.icitap.timsportal.web.custom.PortalLabel;
import us.icitap.timsportal.web.custom.PortalVerticalLayout;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;

@SuppressWarnings("serial")
public class UserEditor extends PortalVerticalLayout implements Button.ClickListener, ConfirmDialogActions {
	private PNativeButton save = new PNativeButton(TimsPortalLabels.getLabel("btn.save"));
	
	private UserForm userForm;
	private OfficerForm officerForm;
	private User user;

	private OfficerWindow officerWindow;

	private PortalBeanLocal portalBean;
	
	public UserEditor(UserForm userForm, OfficerForm officerForm){		
//		setCaption(TimsPortalLabels.getLabel("title.userEditor"));
//		setWidth(Sizeable.SIZE_UNDEFINED, 0);
		setWidth("550px");
//		setHeight("90%");
		setMargin(true);
		setSpacing(true);
		setStyleName("timsportal");
//		addStyleName("white");
		
		PortalLabel title = new PortalLabel(TimsPortalLabels.getLabel("title.userEditor"), Label.CONTENT_XHTML);
		title.setSizeFull();
		title.setStyleName("menu");
		
		addComponent(title);
		
		this.userForm = userForm;
		this.officerForm = officerForm;

		this.officerWindow = new OfficerWindow(officerForm, userForm);
		
		addComponent(userForm);
		addComponent(officerForm);
		
		HorizontalLayout footer = new HorizontalLayout();
		footer.setSizeFull();
		footer.setMargin(false);
		footer.setStyleName("wizard");
		footer.addComponent(save);
		footer.setComponentAlignment(save, Alignment.MIDDLE_RIGHT);
		footer.setWidth("100%");
		
		addComponent(footer);
	}
	
	public void setDataSource(User user){		
		this.user = user;
		userForm.setUser(user);
		userForm.setNoOfficer(user.getOfficer()==null);
		officerForm.setOfficer(user.getOfficer());
		
	}

	@Override
	public void buttonClick(ClickEvent event) {
		// TODO Auto-generated method stub
		Button button = event.getButton();
		if(button==officerForm.search){
			String id = (String) officerForm.officerId.getValue();
			if(id == null || "".equals(id)){
				if(officerWindow.getParent() != null){
					getWindow().showNotification("Window eshte open");
				} else {
					officerWindow.clear();
					getWindow().addWindow(officerWindow);
				}				
			} else {
				Officer officer = this.portalBean.getRmisOfficerById(id);
				officerForm.setOfficer(officer);
				userForm.setNoOfficer(officer==null);
			}			
		} else if(button==officerForm.clear){
			officerForm.setOfficer(null);
			userForm.setNoOfficer(true);
			
		} else if(button==save){
			if (!userForm.isValid()) {
				return;
			}
			TimsPortalConfirmDialog.show(this, this.getWindow(), TimsPortalLabels.getLabel("msg.savingUser"));
		}
		
	}
	
	@Override
	public void attach(){
		super.attach();
		this.portalBean = getApplication().getPortalBean();
		officerForm.search.addListener(this);
		officerForm.clear.addListener(this);
		save.addListener(this);
	}

	@Override
	public void actYes() {

		user = userForm.getUser();
		if(userForm.isNoOfficer())
			user.setOfficer(null);
		else {
			Officer rmisOfficer = officerForm.getOfficer();
			Officer lastOfficer = this.portalBean.getDBOfficerById(rmisOfficer.getOfficerId());
			user.setOfficer(rmisOfficer.equals(lastOfficer)?lastOfficer:rmisOfficer);
			user.setBaseStructure(null);
		}
		user.setLastUpdateDate(new Date());
		user = this.portalBean.saveUser(user);
		setDataSource(user);
		PortalNotification.showTrayMessage("Perdoruesi: <b>"+user.getUsername()+"</b> u ruajt me sukses.", getWindow());
	}

	@Override
	public void actNO() {
		// TODO Auto-generated method stub
		
	}

}
